import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLineEdit, QVBoxLayout, QLabel

class SumCalculator(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('Sum Calculator')
        self.resize(400, 200)

        # 创建布局
        layout = QVBoxLayout(self)

        # 创建标签
        self.label = QLabel('Enter two numbers to calculate their sum.')
        layout.addWidget(self.label)

        # 创建输入框a
        self.line_edit_a = QLineEdit()
        self.line_edit_a.setPlaceholderText('Enter number a...')
        layout.addWidget(self.line_edit_a)

        # 创建输入框b
        self.line_edit_b = QLineEdit()
        self.line_edit_b.setPlaceholderText('Enter number b...')
        layout.addWidget(self.line_edit_b)

        # 创建按钮
        self.button = QPushButton('Run')
        self.button.clicked.connect(self.on_run_clicked)
        layout.addWidget(self.button)

    def on_run_clicked(self):
        try:
            a = int(self.line_edit_a.text())
            b = int(self.line_edit_b.text())
        except ValueError:
            self.label.setText('Please enter valid integers.')
            return

        result = a + b
        self.label.setText(f'The sum of {a} and {b} is {result}.')

def main():
    app = QApplication(sys.argv)
    window = SumCalculator()
    window.show()

    # 捕获未处理的异常
    def excepthook(type, value, tb):
        print("An error occurred:")
        print((type, value, tb))
        sys.__excepthook__(type, value, tb)

    sys.excepthook = excepthook

    sys.exit(app.exec_())

if __name__ == '__main__':
    main()